﻿//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Query Application Block
//===============================================================================

using System;
using System.Linq;
using System.Data;
using System.Xml.Linq;
using System.Globalization;
using System.Collections.Generic;
using EntLibContrib.Query.Commands;

namespace EntLibContrib.Query.QuickStart.CustomCommands
{
    using Configuration;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel;

    /// <summary>
	/// Custom file command to search a set of Basic3 objects by name and filter out deleted objects
	/// </summary>
    [ConfigurationElementType(typeof(ReadBasic3ObjectByNameCommandData))]
    public class ReadBasic3ObjectByNameCommand : XmlFileCommand
	{
		#region Constants
		private const string rootElement = "Basic3Collection";
		private const string objectElement = "Basic3";
		private const string searchElement = "Name";
		#endregion

		#region Construction
		/// <summary>
		/// Initializes a new instance of the <see cref="ReadBasic3ObjectByNameCommand"/> class.
		/// </summary>
		public ReadBasic3ObjectByNameCommand()
		{
		}
		#endregion

		#region Public Methods
		/// <summary>
		/// Executes a read action.
		/// </summary>
		/// <returns>
		/// a DataSet containing the results of the read action
		/// </returns>
		public override DataSet ExecuteForRead()
		{
			// read the xml document
			XDocument document = XDocument.Load(DataFile, LoadOptions.SetBaseUri | LoadOptions.SetLineInfo);
			// find all basic3 objects that have a name element starting with the value of the input parameter
			IEnumerable<XElement> elements = document.Descendants(objectElement).Where(e => ((string)e.Element(searchElement)).ToLower(CultureInfo.CurrentCulture).StartsWith(((string)Parameters[searchElement].Value).ToLower(CultureInfo.CurrentCulture), StringComparison.CurrentCulture));
			// remove all deleted objects
			foreach (XElement element in elements.Descendants("DeletedDate").ToArray())
			{
				if (!string.IsNullOrEmpty(element.Value))
					element.Parent.Remove();
			}
			// build the filtered document
			XDocument filteredDocument = new XDocument(new XElement(rootElement, elements));
			//create the return dataset
			DataSet results = new DataSet(rootElement);
			results.Locale = CultureInfo.CurrentCulture;
			//read in the xml
			results.ReadXml(filteredDocument.CreateReader());
			//return the dataset
			return results;
		}

		/// <summary>
		/// Executes a write action.
		/// </summary>
		public override void ExecuteForWrite()
		{
			throw new NotImplementedException();
		}
		#endregion
	}

    public class ReadBasic3ObjectByNameCommandData : CommandData
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="EntLibContrib.Query.QuickStart.CustomCommands.ReadBasic3ObjectByNameCommandData"/> class.
        /// </summary>
        public ReadBasic3ObjectByNameCommandData()
            : base(typeof(ReadBasic1ObjectSortCommand))
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="EntLibContrib.Query.QuickStart.CustomCommands.ReadBasic3ObjectByNameCommandData"/> class.
        /// </summary>
        public ReadBasic3ObjectByNameCommandData(string name)
            : base(name, typeof(ReadBasic1ObjectSortCommand))
        {
        }

        public override Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel.TypeRegistration GetRegistration()
        {
            return
                new TypeRegistration<ICommand>(
                    () => new ReadBasic1ObjectSortCommand())
                {
                    Lifetime = TypeRegistrationLifetime.Transient,
                    Name = Name,
                    IsPublicName = true
                };
        }
    }
}
